﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Openlab.Tools.CodeGender.Funtions;

namespace Openlab.Tools.CodeGender.Classes
{
    public class ManagerClass : Class
    {
        string entityClass;
        Column idColumn;
        CodeOption option;
        public ManagerClass(CodeOption option)
            : base(option.ManagerClassName, option.NamespaceName)
        {
            this.option = option;
            entityClass = option.ClassName;
            idColumn = option.IdColumn;
            WithDefaultConstructor = false;
            ClassType = "static";
        }

        public override string Body
        {
            get {
                StringBuilder code = new StringBuilder();

                code.AppendLine(GetCacheCode());
                code.AppendLine();

                Function funcGetEntitySet = new GetEntitySetFunc(entityClass, GetIdsFuncName, GetEntitiesFuncName, EntitiesCacheName, idColumn);
                Function funcGetEntities = new GetEntitiesFunc(entityClass, GetEntitiesFuncName, EntitiesCacheName, idColumn);
                Function funcGetEntity = new GetEntityFunc(entityClass, funcGetEntities.Name, EntitiesCacheName, idColumn);

                code.AppendLine(funcGetEntity.Code);
                code.AppendLine();

                code.AppendLine(funcGetEntities.Code);
                code.AppendLine();

                code.AppendLine(funcGetEntitySet.Code);
                code.AppendLine();

                return code.ToString();
            }
        }

        string GetCacheCode()
        {
            StringBuilder code = new StringBuilder();
            string paramEntities = PluralizerHelper.ToPlural(entityClass.ToFirstLower()) + "Cache";
            code.AppendLineFormatWithTabs("static CacheEntityTable<{0}, {1}> {2} = CacheManager.GetCacheEntityTable<{0}, {1}>(\"{2}\", 180, 10000);", 0
                , idColumn.CSTypeName, entityClass, paramEntities);
            code.AppendLineFormatWithTabs("public static CacheEntityTable<{0}, {1}> {2}", 0
                , idColumn.CSTypeName, entityClass, paramEntities.ToFirstUpper());
            code.AppendLineWithTabs("{", 0);
            code.AppendLineFormatWithTabs("get {{ return {0}; }}", 1, paramEntities);
            code.AppendLineFormatWithTabs("set {{ {0} = value; }}", 1, paramEntities);
            code.AppendLineWithTabs("}", 0);
            return code.ToString();
        }

        string GetIdsFuncName
        {
            get {
                return string.Format("Get{0}", idColumn.Name.ToPlural());
            }
        }

        string GetEntitiesFuncName
        {
            get {
                return string.Format("Get{0}", PluralizerHelper.ToPlural(entityClass));
            }
        }

        string EntitiesCacheName
        {
            get {
                return string.Format("{0}Cache", PluralizerHelper.ToPlural(entityClass));
            }
        }
    }
}
